import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bat here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bat  extends Enemy
{
    GreenfootImage buffer = new GreenfootImage(15,7);
    GreenfootImage bats = new GreenfootImage(45,7);
    Zony z;
    int count,pushX=0,pushY=0,offX,offY,health=5,immune=0;
    double x,y,a,b;
    boolean awake=false,dead=false;

    protected void addedToWorld(World world)
    {
        bats=((it)getWorld()).getEn2();  
        Zony it = (Zony) getWorld().getObjects(Zony.class).get(0);
        z=it;
        setImage(buffer);
        buffer.drawImage(bats,0,0);
        x=getX();
        y=getY();
        count=Greenfoot.getRandomNumber(50);
        offX=Greenfoot.getRandomNumber(20)-10;
        offY=Greenfoot.getRandomNumber(20)-10;
    }  

    public void act() 
    {
        if(dead){
            setLocation(getX(),(int)(getY()+a));
            a=a+.25;
            if(!((it)getWorld()).ok(getX(),getY()))
                getWorld().removeObject(this);
            return;
        }
        if(immune>0)immune--;
        if(Math.abs(z.getX()-getX())<100 && z.getY()>getY() && z.getY()<getY()+100)
            awake=true;
        if(awake){
            if(pushX>0)pushX--;
            if(pushX<0)pushX++;
            if(pushY>0)pushY--;
            if(pushY<0)pushY++;
            buffer.clear();
            count++;
            if(count%10 > 5)buffer.drawImage(bats,-15,0);
            else buffer.drawImage(bats,-30,0);
            if(count%50==0){
                offX=Greenfoot.getRandomNumber(20)-10;
                offY=Greenfoot.getRandomNumber(20)-10;
            }
            a = pushX+(z.getX()+offX-getX())/20.0;
            b = pushY+(z.getY()+offY-getY())/20.0;
            x=x+a;
            y=y+b;
            setLocation((int)x,(int)y);  
        }
            
    } 
    
    public void hurt(int x){
        if(immune==0)
            health=health-x;
        immune=5;
        if(health==0 && !dead){
            dead=true;
            a=-2;
        }
        pushY=Greenfoot.getRandomNumber(10)-5;
        if(z.getX()<getX())pushX=5;
        else pushX=-5;
    }
    
    public boolean isDeadly(){
        return !dead;
    }
    
    
            
    
    
    
}
